#import "TTotheAppUser.h"

#import "YOUfanLocationUtils.h"

@interface TTotheAppUser () {
    CGFloat _cunhuaPersonalCellHeight;
    NSArray <NSString *>*_cunhuaPersonalInfos;
    
    CGFloat _cunhuaMyTagsCellHeight;
    NSArray <NSString *>*_cunhuaMyTags;
    
    CGFloat _cunhuaLikeTagsCellHeight;
    NSArray <NSString *>*_cunhuaLikeTags;
}


@end

@implementation TTotheAppUser


- (CGFloat)cunhuaLikeTagsCellHeight {
    if (!_cunhuaLikeTagsCellHeight) {
//        _cunhuaPersonalInfos = [self heiyouPersonalInfos];
        NSMutableArray <NSString *>*heiyou = [NSMutableArray array];
        
        CGFloat heiyouItemH = 24;
        CGFloat hajimiSpaceVH = hajimiPadding08;
        
//        NSString *hajimi;
        CGRect cunhua = CGRectZero;
        CGFloat heiyou1;
        
        if (self.pochuGenderTagRunner.length>0) {
            NSArray <NSString *>*jiucun = [self.pochuGenderTagRunner componentsSeparatedByString:@","];
            for (NSString *cungu in jiucun) {
                heiyou1 = hajimiPadding08 +0 + hajimiPadding08;
                if (cunhua.size.height <=0) {
                    cunhua = CGRectMake(12, 0, heiyou1, heiyouItemH);
                }
                else {
                    if (CGRectGetMaxX(cunhua) + hajimiSpaceVH + heiyou1>0) {
                        cunhua = CGRectMake(12, CGRectGetMaxY(cunhua)+hajimiPadding08, heiyou1, heiyouItemH);
                    }
                    else {
                        cunhua = CGRectMake(CGRectGetMaxX(cunhua)+hajimiPadding08, cunhua.origin.y, heiyou1, heiyouItemH);
                    }
                }
                [heiyou addObject:[NSString stringWithFormat:@"%@=%@", cungu, NSStringFromCGRect(cunhua)]];
                _cunhuaLikeTagsCellHeight = CGRectGetMaxY(cunhua) + 12;
            }
            _cunhuaLikeTags = heiyou.copy;
        }
    }
    return _cunhuaLikeTagsCellHeight;
}


- (CGFloat)cunhuaMyTagsCellHeight {
    if (!_cunhuaMyTagsCellHeight) {
//        _cunhuaPersonalInfos = [self heiyouPersonalInfos];
        NSMutableArray <NSString *>*heiyou = [NSMutableArray array];
        
        CGFloat heiyouItemH = 24;
        CGFloat hajimiSpaceVH = hajimiPadding08;
        CGFloat cunhuaMaxWidth = 0 - 2 * 12;
        
//        NSString *hajimi;
        CGRect cunhua = CGRectZero;
        CGFloat heiyou1;
        
        if (self.pyyouMyTagOrange.length>0) {
            NSArray <NSString *>*jiucun = [self.pyyouMyTagOrange componentsSeparatedByString:@","];
            for (NSString *cungu in jiucun) {
                heiyou1 = 0;
                if (cunhua.size.height <=0) {
                    cunhua = CGRectMake(12, 0, heiyou1, heiyouItemH);
                }
                else {
                    if (CGRectGetMaxX(cunhua) + hajimiSpaceVH + heiyou1>cunhuaMaxWidth) {
                        cunhua = CGRectMake(12, CGRectGetMaxY(cunhua)+hajimiPadding08, heiyou1, heiyouItemH);
                    }
                    else {
                        cunhua = CGRectMake(CGRectGetMaxX(cunhua)+hajimiPadding08, cunhua.origin.y, heiyou1, heiyouItemH);
                    }
                }
                [heiyou addObject:[NSString stringWithFormat:@"%@=%@", cungu, NSStringFromCGRect(cunhua)]];
                _cunhuaMyTagsCellHeight = CGRectGetMaxY(cunhua) + 12;
            }
            _cunhuaMyTags = heiyou.copy;
        }
    }
    return _cunhuaMyTagsCellHeight;
}

- (CGFloat)cunhuaPersonalCellHeight {
    if (!_cunhuaPersonalCellHeight) {
        _cunhuaPersonalInfos = [self heiyouPersonalInfos];
    }
    return _cunhuaPersonalCellHeight;
}

- (NSArray <NSString *>*)heiyouPersonalInfos {
    NSMutableArray <NSString *>*heiyou = [NSMutableArray array];
    
    CGFloat heiyouItemH = 29;
    CGFloat mirchiImageWH = 17;
    CGFloat hajimiSpaceVH = 16;
    CGFloat cunhuaMaxWidth = 0 - 2 * 12;
    
    NSString *hajimi;
    CGRect cunhua = CGRectZero;
    CGFloat heiyou1;
    
    if (self.hayunCodeReder>0) {
        hajimi = [NSString stringWithFormat:@"%ld", (long)self.hayunCodeReder];
        heiyou1 = 7.5 + 17+ hajimiPadding08 +0 + 4 + 10 + 7.5;
        cunhua = CGRectMake(12, 0, heiyou1, heiyouItemH);
        [heiyou addObject:[NSString stringWithFormat:@"profile/ids=%@=%@", hajimi, NSStringFromCGRect(cunhua)]];
    }
    CGFloat jiucun = 0;
    if (self.feshuHeightBlick>0) {
        hajimi = [NSString stringWithFormat:@"%ldcm", (long)self.feshuHeightBlick];
        heiyou1 = 7.5 + 17+ hajimiPadding08 +0 + 7.5;
        
        jiucun = CGRectGetMaxX(cunhua) + hajimiSpaceVH + heiyou1;
        if (jiucun>cunhuaMaxWidth) {
            cunhua = CGRectMake(12, CGRectGetMaxY(cunhua) + hajimiSpaceVH, heiyou1, heiyouItemH);
        }
        else {
            cunhua = CGRectMake(CGRectGetMaxX(cunhua) + hajimiSpaceVH, cunhua.origin.y, heiyou1, heiyouItemH);
        }
        
        [heiyou addObject:[NSString stringWithFormat:@"profile/height_icon=%@=%@", hajimi, NSStringFromCGRect(cunhua)]];
    }
    
    if (self.pochuWeightHotown>0) {
        hajimi = [NSString stringWithFormat:@"%ldkg", (long)self.pochuWeightHotown];
        heiyou1 = 7.5 + mirchiImageWH + hajimiPadding08 +0 + 7.5;
        
        jiucun = CGRectGetMaxX(cunhua) + hajimiSpaceVH + heiyou1;
        if (jiucun>cunhuaMaxWidth) {
            cunhua = CGRectMake(12, CGRectGetMaxY(cunhua) + hajimiSpaceVH, heiyou1, heiyouItemH);
        }
        else {
            cunhua = CGRectMake(CGRectGetMaxX(cunhua) + hajimiSpaceVH, cunhua.origin.y, heiyou1, heiyouItemH);
        }
        
        [heiyou addObject:[NSString stringWithFormat:@"profile/weight_icon=%@=%@", hajimi, NSStringFromCGRect(cunhua)]];
    }
    

    if (self.ayiguRelationshipDurian.length>0) {
        hajimi = [NSString stringWithFormat:@"婚姻状态：%@", self.ayiguRelationshipDurian];
        heiyou1 = 7.5 + 17+ hajimiPadding08 +0 + 7.5;
        
        jiucun = CGRectGetMaxX(cunhua) + hajimiSpaceVH + heiyou1;
        if (jiucun>cunhuaMaxWidth) {
            cunhua = CGRectMake(12, CGRectGetMaxY(cunhua) + hajimiSpaceVH, heiyou1, heiyouItemH);
        }
        else {
            cunhua = CGRectMake(CGRectGetMaxX(cunhua) + hajimiSpaceVH, cunhua.origin.y, heiyou1, heiyouItemH);
        }
        
        [heiyou addObject:[NSString stringWithFormat:@"profile/honey_status=%@=%@", hajimi, NSStringFromCGRect(cunhua)]];
    }
    
//    if (self.fanyouWeightLemon>0) {
//        [heiyou addObject:[NSString stringWithFormat:@"fyanou_yhxq_tz_icon=%ld", (long)self.fanyouWeightLemon]];
//    }
    
    if (self.jiumiHomeCityNameReder.length>0) {
        hajimi = [NSString stringWithFormat:@"家乡：%@", self.jiumiHomeCityNameReder];
        heiyou1 = 7.5 + 17+ hajimiPadding08 +0+ 7.5;
        
        jiucun = CGRectGetMaxX(cunhua) + hajimiSpaceVH + heiyou1;
        if (jiucun>cunhuaMaxWidth) {
            cunhua = CGRectMake(12, CGRectGetMaxY(cunhua) + hajimiSpaceVH, heiyou1, heiyouItemH);
        }
        else {
            cunhua = CGRectMake(CGRectGetMaxX(cunhua) + hajimiSpaceVH, cunhua.origin.y, heiyou1, heiyouItemH);
        }
        
        [heiyou addObject:[NSString stringWithFormat:@"profile/homtown=%@=%@", hajimi, NSStringFromCGRect(cunhua)]];
    }
    
    if (self.whishowCityNameYellow.length>0) {
        hajimi = [NSString stringWithFormat:@"现居地：%@", self.whishowCityNameYellow];
        heiyou1 = 7.5 + mirchiImageWH+ hajimiPadding08 +0 + 7.5;
        
        jiucun = CGRectGetMaxX(cunhua) + hajimiSpaceVH + heiyou1;
        if (jiucun>cunhuaMaxWidth) {
            cunhua = CGRectMake(12, CGRectGetMaxY(cunhua) + hajimiSpaceVH, heiyou1, heiyouItemH);
        }
        else {
            cunhua = CGRectMake(CGRectGetMaxX(cunhua) + hajimiSpaceVH, cunhua.origin.y, heiyou1, heiyouItemH);
        }
        
        [heiyou addObject:[NSString stringWithFormat:@"profile/livecity=%@=%@", hajimi, NSStringFromCGRect(cunhua)]];
    }
    
    if (self.feshuEducationRunman.length>0) {
        NSString *ttathe1 = self.feshuEducationRunman;
        if (self.ayiguCollegeDurian.length>0) {
            ttathe1 = [NSString stringWithFormat:@"%@ · %@", self.ayiguCollegeDurian, self.feshuEducationRunman];
        }
        
        heiyou1 = 7.5 + mirchiImageWH+ hajimiPadding08 +0 + 7.5;
        
        jiucun = CGRectGetMaxX(cunhua) + hajimiSpaceVH + heiyou1;
        if (jiucun>cunhuaMaxWidth) {
            cunhua = CGRectMake(12, CGRectGetMaxY(cunhua) + hajimiSpaceVH, heiyou1, heiyouItemH);
        }
        else {
            cunhua = CGRectMake(CGRectGetMaxX(cunhua) + hajimiSpaceVH, cunhua.origin.y, heiyou1, heiyouItemH);
        }
        
        [heiyou addObject:[NSString stringWithFormat:@"profile/education=%@=%@", ttathe1, NSStringFromCGRect(cunhua)]];
    }
    
    if (self.flagmeJobBlue.length>0) {
        heiyou1 = 7.5 + mirchiImageWH+ hajimiPadding08 +0 + 7.5;
        
        jiucun = CGRectGetMaxX(cunhua) + hajimiSpaceVH + heiyou1;
        if (jiucun>cunhuaMaxWidth) {
            cunhua = CGRectMake(12, CGRectGetMaxY(cunhua) + hajimiSpaceVH, heiyou1, heiyouItemH);
        }
        else {
            cunhua = CGRectMake(CGRectGetMaxX(cunhua) + hajimiSpaceVH, cunhua.origin.y, heiyou1, heiyouItemH);
        }
        
        [heiyou addObject:[NSString stringWithFormat:@"profile/job=%@=%@", self.flagmeJobBlue, NSStringFromCGRect(cunhua)]];
    }
    
    NSMutableArray <NSString *>*ttatheTemp1 = [NSMutableArray array];
    if (self.pochuIncomeBlue.length>0) {
        [ttatheTemp1 addObject:[NSString stringWithFormat:@"年收入%@", self.pochuIncomeBlue]];
        
       
    }
    
    if (ttatheTemp1.count>0) {
        NSString *ttathe5 = [ttatheTemp1 componentsJoinedByString:@" · "];
        heiyou1 = 7.5 + mirchiImageWH+ hajimiPadding08 +0 + 7.5;
        
        jiucun = CGRectGetMaxX(cunhua) + hajimiSpaceVH + heiyou1;
        if (jiucun>cunhuaMaxWidth) {
            cunhua = CGRectMake(12, CGRectGetMaxY(cunhua) + hajimiSpaceVH, heiyou1, heiyouItemH);
        }
        else {
            cunhua = CGRectMake(CGRectGetMaxX(cunhua) + hajimiSpaceVH, cunhua.origin.y, heiyou1, heiyouItemH);
        }
        
        [heiyou addObject:[NSString stringWithFormat:@"profile/income=%@=%@", ttathe5, NSStringFromCGRect(cunhua)]];
    }

    
    _cunhuaPersonalCellHeight = CGRectGetMaxY(cunhua) + 12;

    return heiyou.copy;
}

- (NSString *)ttotheDistanceTome {
    if (!_ttotheDistanceTome) {
        if (self.hajiuLatPink>0 && self.flagmeLonRunman>0 && YOUfanLocationUtils.manager.fyanouLat.floatValue>0 && YOUfanLocationUtils.manager.fyanouLon.floatValue>0) {
            CLLocationCoordinate2D ttothe1 = CLLocationCoordinate2DMake(self.hajiuLatPink, self.flagmeLonRunman);
            
            CLLocationCoordinate2D ttothe2 = CLLocationCoordinate2DMake(YOUfanLocationUtils.manager.fyanouLat.floatValue, YOUfanLocationUtils.manager.fyanouLon.floatValue);
            
            double distance = distanceBetweenCoordinates(ttothe1, ttothe2); // km
            if (distance<1) {
                _ttotheDistanceTome = [NSString stringWithFormat:@"距你%.01fm", distance*1000];
            }
            else {
                _ttotheDistanceTome = [NSString stringWithFormat:@"距你%.01fkm", distance];
            }
        }
        else {
            _ttotheDistanceTome = @"";
        }
    }
    return _ttotheDistanceTome;
}

// 计算两个经纬度之间的距离
double distanceBetweenCoordinates(CLLocationCoordinate2D coord1, CLLocationCoordinate2D coord2) {
    // 地球半径（单位：公里）
    double R = 6371.0;
    
    // 转换经纬度为弧度
    double lat1 = coord1.latitude * M_PI / 180.0;
    double lon1 = coord1.longitude * M_PI / 180.0;
    double lat2 = coord2.latitude * M_PI / 180.0;
    double lon2 = coord2.longitude * M_PI / 180.0;
    
    // 计算纬度差和经度差
    double dLat = lat2 - lat1;
    double dLon = lon2 - lon1;
    
    // Haversine公式
    double a = sin(dLat / 2) * sin(dLat / 2) +
               cos(lat1) * cos(lat2) *
               sin(dLon / 2) * sin(dLon / 2);
    double c = 2 * atan2(sqrt(a), sqrt(1 - a));
    
    // 计算距离
    double distance = R * c; // 单位为公里
    return distance;
}

+ (NSDictionary *)modelCustomPropertyMapper {
    return @{
        @"whishowCOMPLInfoRunner" : @"cOMPLInfo",
        @"pyyouAgeBlick" : @"age",
        @"hayunAppCodeYellow" : @"appCode",
        @"pyyouBirthdayBlick" : @"birthday",
        @"hayunChannelCodeReder" : @"channelCode",
        @"hayunCityCodeHotown" : @"cityCode",
        @"whishowCityNameYellow" : @"cityName",
        @"pochuCitySaleFlagOrange" : @"citySaleFlag",
        @"hayunCodeReder" : @"code",
        @"feshuEducationRunman" : @"education",
        @"flagmeErrFlagHotown" : @"errFlag",
        @"jiumiExcludeSyncPink" : @"excludeSync",
        @"pochuGenderTagRunner" : @"genderTag",
        @"feshuHeightBlick" : @"height",
        @"flagmeHomeCityCodeRunner" : @"homeCityCode",
        @"jiumiHomeCityNameReder" : @"homeCityName",
        @"hayunIconOrange" : @"icon",
        @"miyouIdFieldHotown" : @"id",
        @"pochuIncomeBlue" : @"income",
        @"flagmeJobBlue" : @"job",
        @"hajiuLatPink" : @"lat",
        @"pochuLiveSituationYellow" : @"liveSituation",
        @"flagmeLonRunman" : @"lon",
        @"feshuMakeDateOrange" : @"makeDate",
        @"pochuMobileReder" : @"mobile",
        @"pyyouMyTagOrange" : @"myTag",
        @"whishowNameHotown" : @"name",
        @"pochuNorceRunman" : @"norce",
        @"miyouOfficialHotown" : @"official",
        @"flagmeOsBlick" : @"os",
        @"jiumiOsAppVersionBlick" : @"osAppVersion",
        @"feshuOsNotifyPink" : @"osNotify",
        @"jiumiRecommendScoreRunner" : @"recommendScore",
        @"hajiuSaleFlagRunman" : @"saleFlag",
        @"feshuSexRunner" : @"sex",
        @"hajiuSignatureHotown" : @"signature",
        @"jiumiStatusBlue" : @"status",
        @"jiumiSystemVersionHotown" : @"systemVersion",
        @"hajiuTypeBlick" : @"type",
        @"whishowVoiceSignatureBlue" : @"voiceSignature",
        @"whishowVoiceSignatureDurationHotown" : @"voiceSignatureDuration",
        @"pochuWeightHotown" : @"weight",
        @"hajimiHavecarBanana" : @"haveCar",
        @"ayiguCollegeDurian"  : @"college",
        @"ayiguRelationshipDurian" : @"relationship"
    };
};

- (void)ttotheRefreshTags:(NSInteger)index {
    if (index == 0) {
        _cunhuaMyTagsCellHeight = 0;
    }
    else {
        _cunhuaLikeTagsCellHeight = 0;
    }
}

@end
